<template>
  <div>
    <div>
      <we-button
        v-if="baseCode"
        @click="dialogFormVisibleOpen"
        type="primary"
        size="mini"
      >
        <em class="el-icon-plus"></em> 新增
      </we-button>
      <we-button
        v-if="baseCode"
        @click="handleClick(currentRow)"
        type="primary"
        size="mini"
      >
        <em class="el-icon-edit"></em> 修改
      </we-button>
      <we-button v-if="baseCode" @click="deleteRow" type="primary" size="mini">
        <em class="el-icon-delete"></em> 删除
      </we-button>
      <span v-if="baseName">当前人员：【 {{baseName}} 】</span>
    </div>

    <!-- 表格 -->
    <el-table
      size="mini"
      :header-cell-style="rowClass"
      :data="tableData"
      height="116px"
      border
      highlight-current-row
      resizable
      ref="singleTable"
      @row-dblclick="handleClick"
      @current-change="handleCurrentChange"
      style="width: 100%; margin-top: 5px"
    >
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column
        prop="investigationTime"
        width="150"
        :show-overflow-tooltip="true"
        label="考察(考核)日期"
      >
      </el-table-column>
      <el-table-column
        prop="investigationType_dictText"
        width="150"
        :show-overflow-tooltip="true"
        label="考察(考核)类别"
      >
      </el-table-column>
      <el-table-column
        prop="investigationName"
        width="150"
        :show-overflow-tooltip="true"
        label="考察(考核)名称"
      >
      </el-table-column>
      <el-table-column
        prop="investigationResult_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="考察(考核)结果"
      >
      </el-table-column>
      <el-table-column
        prop="investigationMan"
        :show-overflow-tooltip="true"
        width="150"
        label="考察(考核)组织及考察人"
      >
      </el-table-column>
      <el-table-column
        prop="investigationOpinion"
        :show-overflow-tooltip="true"
        width="150"
        label="考察(考核)意见"
      >
      </el-table-column>
      <el-table-column
        prop="investigationSection"
        :show-overflow-tooltip="true"
        width="150"
        label="考察(考核)区间"
      >
      </el-table-column>
      <el-table-column
        prop="personalSummary"
        :show-overflow-tooltip="true"
        label="个人总结"
      >
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <we-button @click="handleClick(scope.row)" type="primary" size="mini" >详情</we-button>
          <we-button type="danger" size="mini" @click="deleteSh(scope.row)">删 除</we-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="table_tools">
      <we-Pagination
        :page="pagination.pageNum"
        :pageSize="pagination.pageSize"
        :total="pagination.total"
        @currentChange="currentChange($event)"
        >></we-Pagination
      >
    </div>
    <!-- 表格 -->

    <!-- 新增弹窗 ↓↓↓↓↓↓↓↓↓↓-->
    <el-dialog
      title="新增组织考察(考核)子集"
      :visible.sync="dialogFormVisible"
      @close="handleOffDialog"
      width="45%"
      append-to-body
    >
      <el-form :model="form" label-width="170px" size="mini" ref="form">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)日期">
              <el-date-picker
                class="inputW"
                v-model="form.investigationTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考察(考核)类别">
              <el-select
                class="inputW"
                v-model="form.investigationType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in investigationCategory"
                  :key="item.uuid"
                  :label="item.name"
                  :value="item.uuid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)名称">
              <el-input
                class="inputW"
                v-model="form.investigationName"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考察(考核)结果">
              <el-select
                class="inputW"
                v-model="form.investigationResult"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in investigationResult"
                  :key="item.uuid"
                  :label="item.name"
                  :value="item.uuid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)组织及考察人">
              <el-input
                class="inputW"
                v-model="form.investigationMan"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考察(考核)意见">
              <el-input
                class="inputW"
                v-model="form.investigationOpinion"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)区间">
              <el-input
                class="inputW"
                v-model="form.investigationSection"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="个人总结">
              <el-input
                class="inputW"
                v-model="form.personalSummary"
                type="textarea"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <we-button size="mini" @click="handleOffDialog"
          >取 消</we-button
        >
        <we-button size="mini" type="primary" @click="addRe('form')"
          >新 增</we-button
        >
      </div>
    </el-dialog>
    <!-- 新增弹窗 ↑↑↑↑↑↑↑↑↑↑ -->
    <!-- 编辑弹窗 ↓↓↓↓↓↓↓↓↓↓-->
    <el-dialog
      title="编辑组织考察(考核)子集"
      :visible.sync="dialogVisible"
      width="45%"
      append-to-body
    >
      <el-form :model="form" label-width="170px" size="mini" ref="form">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)日期">
              <el-date-picker
                class="inputW"
                v-model="form.investigationTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考察(考核)类别">
              <el-select
                class="inputW"
                v-model="form.investigationType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in investigationCategory"
                  :key="item.uuid"
                  :label="item.name"
                  :value="item.uuid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)名称">
              <el-input
                class="inputW"
                v-model="form.investigationName"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考察(考核)结果">
              <el-select
                class="inputW"
                v-model="form.investigationResult"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in investigationResult"
                  :key="item.uuid"
                  :label="item.name"
                  :value="item.uuid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)组织及考察人">
              <el-input
                class="inputW"
                v-model="form.investigationMan"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考察(考核)意见">
              <el-input
                class="inputW"
                v-model="form.investigationOpinion"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考察(考核)区间">
              <el-input
                class="inputW"
                v-model="form.investigationSection"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="个人总结">
              <el-input
                class="inputW"
                v-model="form.personalSummary"
                type="textarea"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <we-button size="mini" @click="dialogVisible = false">取 消</we-button>
        <we-button size="mini" type="primary" @click="updRe('form')"
          >编 辑</we-button
        >
      </div>
    </el-dialog>
    <!-- 编辑弹窗 ↑↑↑↑↑↑↑↑↑↑ -->
  </div>
</template>

<script>
import WeButton from "@/components/WeButton";
import WePagination from "@/components/WePagination";
import { getAction, postAction, deleteAction, putAction } from "@/api/manage";
import {bus} from "@/utils/eventBus";
import {opDictNew} from "@/api/apiUrl";
export default {
  components: {
    WeButton,
    WePagination,
  },
  data() {
    return {
      selListParameter: { baseCode: "" }, // 列表查询参数
      pagination: {
        total: 10, // 总数据条数
        pageSize: 10, // 每页条数
        pageNum: 1, // 当前页
      }, // 列表分页数据
      tableData: [],
      dialogFormVisible: false,
      dialogVisible: false,
      currentRow: null,
      form: {
        investigationTime: "",
        investigationType: "",
        investigationName: "",
        investigationResult: "",
        investigationOpinion: "",
        investigationMan: "",
        investigationSection: "",
        personalSummary: "",
        uuid: "",
        baseCode: "",
        sysOrgCode: "",
        createUser: "",
        uuidList:[],
      },
      value: "",
      value1: "",

      options: [],
      investigationCategory: [], // 特定考核
      investigationResult: [],
      baseCode: "",
      baseName: "",
    };
  },
  watch: {
    dialogFormVisible() {
      if (this.dialogFormVisible === false) {
        if (this.$store.state.investigation === true) {
          this.$store.commit("changeInvestigation", false);
        }
      }
    },
    dialogVisible() {
      if (this.dialogVisible === false) {
        if (this.$store.state.investigation === true) {
          this.$store.commit("changeInvestigation", false);
        }
      }
    },
    "$store.state.baseCode"(newval) {
      this.baseCode = newval;
      this.getList();
    },
    '$store.state.baseName'(newval) {
      this.baseName  = newval
    },
    "$store.state.investigation"(newval) {
      if (newval === true) {
        if (this.$store.state.updateSubset === true) {
          this.dialogVisible = true;
        } else {
          this.dialogFormVisible = true;
        }
        this.$store.commit("changeUpdateSubset", false);
        this.baseCode = this.$store.state.baseCodes;
        this.getInvestigationCategory();
        this.getInvestigationResult();
      }
    },
  },
  methods: {
    clickSubset() {
      this.baseCode = this.$store.state.baseCode;
      this.getInvestigationCategory();
      this.getInvestigationResult();
      this.getList();
    },
    batchAdd(data){
      this.dialogFormVisible = true
      this.ruleForm.uuidList = data
    },
    /**
     *删除
     */
    deleteRow() {
      this.$confirm("是否要删除这条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            deleteAction("/opOrganizationInvestigation/delete", {
              uuids: [this.currentRow.uuid],
            }).then((res) => {
              if (res.code === 200) {
                this.thatMessage("删除成功", "success");
                this.getList();
              } else {
                this.thatMessage("操作失败", "warning");
              }
            });
          })
          .catch(() => {
            this.thatMessage("已取消删除", "info");
          });
    },
    // 查询列表
    getList() {
      let self = this;
      const dataZ = this.selListParameter;
      dataZ.baseCode = this.baseCode;
      dataZ.size = this.pagination.pageSize;
      dataZ.page = this.pagination.pageNum;
      postAction(
        "/opOrganizationInvestigation/opOrganizationInvestigationSearch",
        dataZ
      )
        .then((result) => {
          // if (!result.data.list.length && this.pagination.pageNum !== 1) {
          //   this.pagination.pageNum -= 1
          //   this.getList()
          // }
          self.pagination.total = result.data.total;
          self.tableData = result.data.list;
        })
        .catch(() => {});
    },

    /**
     * 新增窗打开
     */
    dialogFormVisibleOpen() {
      this.thatFormClean();
      this.dialogFormVisible = true;
    },

    /**
     *新增点击
     */
    addRe(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const data = this.form;
          data.baseCode = this.baseCode;
          data.sysOrgCode = "0a590ef720f44d96ab5bebf3c7db0de3";
          data.createUser = "0a590ef720f44d96ab5bebf3c7db0de3";
          if (this.$store.state.investigation === true) {
            for (let i = 0; i < this.baseCode.length; i++) {
              data.baseCode = this.baseCode[i];
              postAction("/opOrganizationInvestigation/add", data)
                .then((result) => {
                  if (result.code === 200) {
                    this.dialogFormVisible = false;
                    bus.$emit('handleDialog')
                    this.thatMessage("操作成功", "success");
                    this.thatFormClean();
                  } else {
                    this.thatMessage(result.msg, "warning");
                  }
                })
                .catch(() => {
                  this.thatMessage("操作失败", "warning");
                });
            }
          } else {
            postAction("/opOrganizationInvestigation/add", data)
              .then((result) => {
                if (result.code === 200) {
                  this.dialogFormVisible = false;
                  bus.$emit('handleDialog')
                  this.thatMessage("操作成功", "success");
                  this.getList();
                  this.thatFormClean();
                } else {
                  this.thatMessage(result.msg, "warning");
                }
              })
              .catch(() => {
                this.thatMessage("操作失败", "warning");
              });
          }
        }
      });
    },

    handleCurrentChange(val) {
      this.currentRow = val;
    },
    /**
     * 编辑窗打开
     */
    handleClick(row) {
      if (!row) {
        this.$message("请选择要编辑的数据！");
        return;
      }
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },

    /**
     *编辑点击
     */
    updRe(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.form));
          data.updateUser = "0a590ef720f44d96ab5bebf3c7db0de3";
          postAction("/opOrganizationInvestigation/update", data)
            .then((result) => {
              if (result.code === 200) {
                this.dialogVisible = false;
                bus.$emit('handleDialog')
                this.thatMessage("操作成功", "success");
                this.getList();
              } else {
                this.thatMessage(result.msg, "warning");
              }
            })
            .catch(() => {
              this.thatMessage("操作失败", "warning");
            });
        }
      });
    },
    handleOffDialog() {
      this.dialogFormVisible = false
      bus.$emit('handleDialog')
    },
    /**
     * 删除数据
     */
    deleteSh(row) {
      this.$confirm("是否要删除这条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteAction("/opOrganizationInvestigation/delete", {
            uuids: [this.currentRow.uuid],
          }).then((res) => {
            if (res.code === 200) {
              this.thatMessage("删除成功", "success");
              this.getList();
            } else {
              this.thatMessage("操作失败", "warning");
            }
          });
        })
        .catch(() => {
          this.thatMessage("已取消删除", "info");
        });
    },

    /**
     * 获取考察(考核)类别字典
     */
    async getInvestigationCategory() {
      const result = await getAction(`${opDictNew.findTreeByType}?type=rs_cb`)
      this.investigationCategory = result.data;
      // const result = await getAction('opDictItem/getDictItemListByCode', {
      //   code: 'investigation_category'
      // })
      // this.investigationCategory = result.data.list
    },

    /**
     * 获取考察(考核)结果字典
     */
    async getInvestigationResult() {
      const result = await getAction(`${opDictNew.findTreeByType}?type=rs_cc`)
      this.investigationResult = result.data;
      // const result = await getAction('opDictItem/getDictItemListByCode', {
      //   code: 'investigation_result'
      // })
      // this.investigationResult = result.data.list
    },

    rowClass({ row, rowIndex }) {
      return "background:rgba(248, 248, 249, 1);color:#000;";
    },
    handleChange() {},
    handleClose() {},

    // 本页面弹窗
    thatMessage(message, type) {
      this.$message({
        message: message,
        type: type,
      });
    },
    // 清空form
    thatFormClean() {
      this.form = {
        investigationTime: "",
        investigationType: "",
        investigationName: "",
        investigationResult: "",
        investigationOpinion: "",
        investigationMan: "",
        investigationSection: "",
        personalSummary: "",
        uuid: "",
        baseCode: "",
        sysOrgCode: "",
        createUser: "",
      };
    },

    // 当前页改变
    currentChange(event) {
      this.pagination.pageNum = event;
      this.getList();
    },
  },
};
</script>

<style scoped>
.table_tools {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
